package com.zsn.ai.config;

import com.zsn.ai.constants.SystemConstants;
import com.zsn.ai.tools.CourseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: ai-demo
 * @ClassName CommonConfiguration
 * @description:
 * @author: zsn
 * @create: 2025-09-16 00:01
 * @Version 1.0
 **/
@Configuration
public class CommonConfiguration {

    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }
    @Bean
    public ChatMemory chatMemory(MysqlChatMemory mysqlChatMemory) {
        return mysqlChatMemory;
    }

//    @Bean
//    public ChatMemory chatMemory(ChatMemoryRepository repository) {
//        return MessageWindowChatMemory.builder()
//                .chatMemoryRepository(new InMemoryChatMemoryRepository())
//                .maxMessages(10) //设置窗口大小，指拼接prompt的时候将最近的多少条数据一起发送
//                .build();
//    }
//    @Bean                               //参数在容器中自动获取，无需显式注入
//    public ChatMemoryRepository chatMemoryRepository(RedisChatMemoryRepositoryDialect dialect) {
//        return new RedisChatMemoryRepository(dialect);
//    }

    @Bean
    public ChatClient chatClient(OpenAiChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder(model)
                .defaultOptions(ChatOptions.builder().model("qwen-omni-turbo").build())
                .defaultSystem("你是一个热心的语音智能助手，你的名字叫小沐沐，请以小沐沐的身份回答用户问题")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory)
                                .build()) // 配置日志advisor
                .build();
    }

    @Bean
    public ChatClient gameChatClient(OpenAiChatModel model , ChatMemory chatMemory){
        return ChatClient
                .builder(model)
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory)
                                .build()) // 配置日志advisor
                .build();
    }

    @Bean
    public ChatClient serviceChatClient(OpenAiChatModel model, ChatMemory chatMemory, CourseTools courseTools) {
        return ChatClient
                .builder(model)
                .defaultSystem(SystemConstants.SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory)
                                .build()) // 配置日志advisor
                .defaultTools(courseTools)
                .build();
    }
    /**
     * 配置pdf文件对话用ChatClient对象，用于处理用户输入的pdf文件，并返回处理结果
     * @param model 使用OpenAI的模型
     * @param mysqlChatMemory 通过数据库进行会话历史存储
     * @param vectorStore 通过向量库进行相似度搜索
     * @return
     */
    @Bean
    public ChatClient pdfChatClient(OpenAiChatModel model, MysqlChatMemory mysqlChatMemory,  VectorStore vectorStore) {
        return ChatClient
                .builder(model)// 选择模型
                .defaultSystem("请根据上下文回答问题，遇到上下文没有的问题，不要随意编造。")// 系统设置
                .defaultAdvisors(new SimpleLoggerAdvisor())// 添加日志记录
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(mysqlChatMemory).build())// 添加会话记忆功能
                .defaultAdvisors(QuestionAnswerAdvisor.builder(vectorStore).// 添加向量库
                        searchRequest(SearchRequest.builder().similarityThreshold(0.6).// 设置相似度阈值
                        topK(2).// 设置返回文档数量
                        build()).build())
                .build();
    }
}